#include "std.h"

int lamp_is_lit, reboot_time, time_from_reset, last_reset_cycle;
int list_length;
object weath;

reset(arg)
{
    call_other("room/test", "short", 0)
    ;
  if(!present("clock", find_object("room/test.c"))) {
    weath=clone_object("obj/autoshut");
    move_object(weath, "room/test.c");
  }
  if(!present("candles", this_object())) {
    weath=clone_object("obj/candles");
   move_object(weath,this_object());
  }
    if (time_from_reset)
	last_reset_cycle = time() - time_from_reset;
    time_from_reset = time();
    if (arg)
	return;
    /* Next if-statement is to prevent against wizards. */
    if (reboot_time == 0)
/*
	call_out("call_combine", 10);
*/
if(!arg)
    set_light(1);
    reboot_time = time();
}

init()
{
   rem_agg();
    add_action("west"); add_verb("west");
    add_action("open"); add_verb("open");
    add_action("push"); add_verb("push");
    add_action("push"); add_verb("press");
    add_action("close"); add_verb("close");
    add_action("east"); add_verb("east");
    add_action("north"); add_verb("north");
    add_action("pray"); add_verb("pray");
    add_action("pray", "regenerate");
    add_action("south"); add_verb("south");
}

short() {
    return "Village church";
}

long(str)
{
    if (str == "clock") {
	int i, j;
	write("The clock shows ");
	i = time() - reboot_time;
	j = i / 60 / 60 / 24;
	if (j == 1)
	    write("1 day ");
	else if (j > 0)
	    write(j + " days ");
	i -= j * 60 * 60 * 24;
	j = i / 60 / 60;
	if (j == 1)
	    write("1 hour ");
	else if (j > 0)
	    write(j + " hours ");
	i -= j * 60 * 60;
	j = i / 60;
	if (j == 1)
	    write("1 minute ");
	else if (j > 0)
	    write(j + " minutes ");
	i -= j * 60;
	if (i == 1)
	    write("1 second");
	else if (i > 0)
	    write(i + " seconds");
	write("\n");
	if (call_other(this_player(), "query_level") < 20)
	    return;
	write("Time since reset is " + (time() - time_from_reset) +
	      " seconds.\n");
	if (last_reset_cycle)
	    write("Reset cycle: " + last_reset_cycle + "\n");
	write("Free block list length: " + list_length + "\n");
	return;
    }
    if (str == "door") {
	if (!call_other("room/elevator", "query_door", 0) &&
	    call_other("room/elevator", "query_level", 0))
	    write("The door is open.\n");
	else
	    write("The door is closed.\n");
	return;
    }
    if (str == "pit") {
	write("In the middle of the church is a deep pit.\n"+
	      "It was used for sacrifice in the old times, but nowadays\n" +
	      "it is only left for tourists to look at.\n");
	return;
    }
   if (str == "button") { 
         write("The button on the wall is somehow associated with the \n"+
          "elevator's operation. Maybe you can push it. \n");
          if (lamp_is_lit) write("The lamp beside the elevator is lit.\n");

       return;
      }
    write("You are in the local village church.\nThere is a huge pit in the center,\n" +
	 "and a door in the west wall. There is a button beside the door.\n");
    write("This church has the service of reviving ghosts. Dead people come\n");
    write("to the church and pray.\n");
    write("There is a clock on the wall.\n");
    write("The are exits to the south, east and north.\n");
    if (lamp_is_lit)
        write("The lamp beside the elevator is lit.\n");

}

id(str) {
    return str == "door" || str == "pit" || str == "clock"||str=="button";
}

xyzzy() {
    write("Everything shimmers.\n");
    write("You wake up elsewhere...\n");
    call_other(this_player(), "move_player", "elsewhere#room/test");
}

west() {
    if (call_other("room/elevator", "query_door", 0) ||
	call_other("room/elevator", "query_level", 0) != 2) {
	write("The door is closed.\n");
	return 1;
    }
    call_other(this_player(), "move_player", "west#room/elevator");
    return 1;
}

open(str)
{
    if (str != "door")
	return 0;
    if (call_other("room/elevator", "query_level", 0) != 2) {
	write("You can't when the elevator isn't here.\n");
	return 1;
    }
    call_other("room/elevator", "open_door", "door");
    return 1;
}

close(str)
{
    if (str != "door")
	return 0;
    call_other("room/elevator", "close_door", "door");
    return 1;
}

push(str)
{
    if (!str || str != "button")
	return 0;
    if (call_other("room/elevator", "call_elevator", 2))
	lamp_is_lit = 1;
      write("You push the button.\n");
    return 1;
}

elevator_arrives()
{
    say("The lamp on the button beside the elevator goes out.\n");
    lamp_is_lit = 0;
}

pray() {
    if(this_player()->query_guild_name() == "shardak")
    {
      object mark;
      if(mark = present("shardak_mark", this_player()))
      {
        write("You may not pray at this most holiest of places.\n");
        return 1;
      }
    }
    return call_other(this_player(), "remove_ghost", 0);
}

prevent_look_at_inv(str)
{
    return str != 0;
}

south() {
    call_other(this_player(), "move_player", "south#room/vill_green");
    return 1;
}

north()
{
    call_other(this_player(),"move_player","north#room/townh");
    return 1;
}

east() 
{
/*
    call_other(this_player(), "move_player", "east#players/boltar/sprooms/clinic/lobby");
*/
    call_other(this_player(), "move_player", "east#room/clinic");
    return 1;
}

/*
 * The next call of combine_free_list() is dependant of how long the free list
 * is. However, resorting the free list goes fast if the list is already
 * sorted.
 */
call_combine() {
    int tmp;
    tmp = time();
    list_length = combine_free_list();
    if (time() - tmp > 20)
	shout("Game driver shouts: Sorry for the delay. I had some garbage collection to do.\n");
    if (list_length < 1000)
	call_out("call_combine", 10);
    else if (list_length < 5000)
	call_out("call_combine", 30);
    else
	call_out("call_combine", 60 * 2);
}

query_drop_castle() {
    return 1;
}
query_reboot_time(){
       return reboot_time;
}
nomask string valid_read(string path) { }
nomask string valid_write(string path) { }
rem_agg() {
   object what;
   object ob;
   what = this_object();
   ob = first_inventory(what);
   if (ob) {
      
      while(ob) {
          if(ob->query_aggressive()) { 
            write(ob->short()+" vanishes in a puff of smoke.\n");
            move_object(ob,"room/south/sforst29");
          }
         ob = next_inventory(ob);
       }
   }
   return 1;
}
